﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;

namespace TimHeuer.WindowsLiveWriter.S34Writer
{
    public partial class CustomListBox : ListBox
    {
        public CustomListBox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The number of pixels to pad around the interior edge of each item.
        /// Default is 1 pixel.
        /// </summary>
        public int ItemPadding
        {
            get { return _itemPadding; }
            set { _itemPadding = value; }
        }
        private int _itemPadding = 1;

        /// <summary>
        /// Text Brush
        /// </summary>
        protected static SolidBrush _textBrush = new SolidBrush(SystemColors.WindowText);
        private StringFormat _sf = null;

        /// <summary>
        /// Draws the item with the icon
        /// </summary>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            object item;
            Rectangle textBounds = new Rectangle(e.Bounds.X,
                e.Bounds.Y + _itemPadding,
                e.Bounds.Width - (_itemPadding * 2),
                e.Bounds.Height - (_itemPadding * 2));
            // Draw the text
            if ((e.State & DrawItemState.Disabled) == DrawItemState.Disabled)
            {
                _textBrush.Color = SystemColors.GrayText;
            }
            else if ((e.State & DrawItemState.Selected) == DrawItemState.Selected && Focused)
            {
                _textBrush.Color = SystemColors.HighlightText;
            }
            else if ((e.State & DrawItemState.Selected) == DrawItemState.Selected && !Focused)
            {
                // Custom draw a non-focused selected item
                _textBrush.Color = SystemColors.WindowText;
                e.Graphics.FillRectangle(new SolidBrush(BackColor), e.Bounds);
                e.Graphics.DrawRectangle(new Pen(SystemColors.Highlight), e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
            }
            else
            {
                _textBrush.Color = SystemColors.WindowText;
            }
            if (_sf == null)
            {
                _sf = new StringFormat(StringFormat.GenericDefault);
                _sf.LineAlignment = StringAlignment.Center;
                _sf.FormatFlags |= StringFormatFlags.NoWrap;
            }
            try
            {
                if ((Items.Count > 0) && (e.Index > -1))
                {
                    item = Items[e.Index];
                    if (item != null)
                    {
                        e.Graphics.DrawString(item.ToString(), e.Font, _textBrush, textBounds, _sf);
                    }
                }
            }
            catch
            {
                string text = (e.Index != -1) ? Items[e.Index].ToString() : "-ERROR-";
                e.Graphics.DrawString(text, e.Font, _textBrush, textBounds, _sf);
            }
            base.OnDrawItem(e);
        }
    }
}